﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using DAO;
using BUS;
using DTO;



namespace vantai
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }
        Data dt=new Data();
        
        public static string constringFromLogin;
        //của tao
        public static bool KT;
        //kết thúc
        private static string maNV;

        public static string MaNV
        {
            get { return maNV; }
            set { maNV = value; }
        }

        private static int loaiNV;

        public static int LoaiNV
        {
            get { return loaiNV; }
            set { loaiNV = value; }
        }

        private bool ischeckedDBNoneFixed;

        public bool IscheckedDBNoneFixed
        {
            get { return ischeckedDBNoneFixed; }
            set { ischeckedDBNoneFixed = value; }
        }
        private bool ischeckedDBFixed;

        public bool IscheckedDBFixed
        {
            get { return ischeckedDBFixed; }
            set { ischeckedDBFixed = value; }
        }
        private static string tenNV;

        public static string TenNV
        {
            get { return tenNV; }
            set { tenNV = value; }
        }

        private void DangNhap()
        {
            if (rdbDBchuafix.Checked)
            {
                dt.Cs = "Data Source=DUCNHA;Initial Catalog=hungdong2chuaxuly;Integrated Security=True";
                constringFromLogin = "Data Source=DUCNHA;Initial Catalog=hungdong2chuaxuly;Integrated Security=True";
                //của tao,nếu có trục trặc nói tao nha



                //dt.Cs = "Data Source=tuongvanthanh;Initial Catalog=DBTransportHDNoneFixed;Integrated Security=True";
                //constringFromLogin = "Data Source=tuongvanthanh;Initial Catalog=DBTransportHDNoneFixed;Integrated Security=True";


                KT = true;
            }
            else
            {
                if (rdbDBdafix.Checked)
                {
                    dt.Cs = "Data Source=DUCNHA;Initial Catalog=HUNGDONG2;Integrated Security=True";
                    constringFromLogin = "Data Source=DUCNHA;Initial Catalog=HUNGDONG2;Integrated Security=True";
                    //của tao
                    KT = false;

                    //dt.Cs = "Data Source=tuongvanthanh;Initial Catalog=DBTransportHDFixed;Integrated Security=True";
                    //constringFromLogin = "Data Source=tuongvanthanh;Initial Catalog=DBTransportHDFixed;Integrated Security=True";

                    
                }
            }

            if (txtUsername.Text.Length == 0 || txtPassword.Text.Length == 0)
            {
                MessageBox.Show("Không được bỏ trống username hay password", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //  dt = new Data();

            NhanVienBUS nv = new NhanVienBUS();
            NhanVienDTO nvdto = new NhanVienDTO();
            nvdto.MaNV = txtUsername.Text;
            nvdto.Password = txtPassword.Text;
            DataRow dr = null;
            try
            {

                dr = nv.LayThongTinNhanVien(nvdto);
            }
            catch (SqlException sl)
            {
                MessageBox.Show(sl.Message.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (dr == null)
            {
                MessageBox.Show("Username hoặc mật khẩu không đúng", "Thông báo");
            }
            else
            {
                TenNV = dr[2].ToString();
                MaNV = dr[0].ToString();
                LoaiNV = Int32.Parse(dr[3].ToString());
                this.Hide();
                MainForm mf = new MainForm();
                mf.ShowDialog();
                this.Show();

            }
        }


        private void btnDangNhap_Click(object sender, EventArgs e)
        {

            DangNhap();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void Login_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DangNhap();
            }
            else
            {
                if (e.KeyCode == Keys.T)
                {
                    this.Close();
                }
            }
        }




    }
}
